package com.xuetang9.fujian.view.panel;

import com.xuetang9.fujian.model.Group;
import com.xuetang9.fujian.model.User;
import com.xuetang9.fujian.view.ChatFrame;
import com.xuetang9.fujian.view.WindowManager;
import com.xuetang9.fujian.view.layout.BlockLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @describe: lianxiren list
 * @author:fujian
 * @date:2021/6/9
 * @Version: V1.0
 * @Package: com.xuetang9.fujian.view.panel
 */
@SuppressWarnings("all")
public class RelationListPanel extends JPanel {
    private Map<JButton, List<RelationPanel>> relationMapping = new HashMap<>();
    public RelationListPanel(){
        init();
    }

    private void init() {
        setLayout(new BlockLayout());
        setPreferredSize(new Dimension(290,0));
        creatRelationPanel();
        setBackground(Color.WHITE);
    }

    public void loadGroups(Map<Group, List<User>> groupsPersons){
        this.removeAll();
        for (Map.Entry<Group, List<User>> entry : groupsPersons.entrySet()) {
            creatRelationPanel(entry.getKey(),entry.getValue());
        }
    }

    private void creatRelationPanel(Group group,List<User> relationPersons){
        List<RelationPanel> groups = new ArrayList<>();
        JButton groupTitleButton = new JButton(group.getGroupName());
        groupTitleButton.setContentAreaFilled(false);
        groupTitleButton.setBorderPainted(false);
        groupTitleButton.setFocusPainted(false);
        groupTitleButton.setHorizontalAlignment(JButton.LEFT);
        add(groupTitleButton);
        for (User person : relationPersons) {
            RelationPanel relationPanel = new RelationPanel(person);
            add(relationPanel);
            groups.add(relationPanel);
            relationPanel.addMouseListener(new RelationPanelMouseHandler());
        }
        relationMapping.put(groupTitleButton,groups);
        groupTitleButton.addActionListener(new GroupTitleButtonHandler());

    }

    private void creatRelationPanel() {
        JButton groupTitleButton = new JButton("test");
        groupTitleButton.setContentAreaFilled(false);
        groupTitleButton.setBorderPainted(false);
        groupTitleButton.setFocusPainted(false);
        groupTitleButton.setHorizontalAlignment(JButton.LEFT);
        add(groupTitleButton);
        List<RelationPanel> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setAccount("100" + (i + 1));
            user.setNickName("frendsId" + (i+1));
            user.setPassword("123456");
            user.setSignature("signature123");
            RelationPanel relationPanel = new RelationPanel(user);
            add(relationPanel);
            list.add(relationPanel);
            relationPanel.addMouseListener(new RelationPanelMouseHandler());
        }
        relationMapping.put(groupTitleButton,list);
        groupTitleButton.addActionListener(new GroupTitleButtonHandler());
    }

    private class GroupTitleButtonHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            List<RelationPanel> groups = relationMapping.get(e.getSource());
            if (groups != null){
                for (RelationPanel relationPanel : groups) {
                    relationPanel.setVisible(!relationPanel.isVisible());
                }
            }
        }
    }


    private class RelationPanelMouseHandler extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2){
                RelationPanel relationPanel = (RelationPanel) e.getSource();
                WindowManager.getChatFrame(relationPanel.getRelationPerson());
            }
        }
    }

}
